home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 2.4 KB | 101 lines | [TEXT/CWIE] |
- // Justification.cp
-
- #ifndef Justification_h
- #include "Justification.h"
- #endif
- #ifndef Assert_h
- #include "Assert.h"
- #endif
- #ifndef Rectangle_h
- #include "Rectangle.h"
- #endif
-
- Justification::Justification( uint32 theDenominator,
- uint32 leading,
- uint32 trailing )
- : denominator( theDenominator ),
- leadingPart( leading ),
- stretchingPart( theDenominator - leading - trailing )
- {
- Assert( theDenominator > 0 );
- Assert( leading <= theDenominator );
- Assert( trailing <= theDenominator );
- Assert( leading + trailing <= theDenominator );
- }
-
- uint32 Justification::LeadingPortion( uint32 total, uint32 used ) const
- {
- uint32 slop = ( total > used ) ? total - used : 0;
-
- Assert( CanMultiply( slop, leadingPart ) );
-
- return slop * leadingPart / denominator;
- }
-
- uint32 Justification::TrailingPortion( uint32 total, uint32 used ) const
- {
- // This section gets the rounding error
-
- uint32 slop = ( total > used ) ? total - used : 0;
- Assert( CanMultiply( slop, leadingPart ) );
- Assert( CanMultiply( slop, stretchingPart ) );
-
- return slop - (slop * leadingPart / denominator)
- - (slop * stretchingPart / denominator);
- }
-
- uint32 Justification::StretchPortion( uint32 total, uint32 used ) const
- {
- uint32 slop = ( total > used ) ? total - used : 0;
-
- Assert( CanMultiply( slop, stretchingPart ) );
-
- return slop * stretchingPart / denominator;
- }
-
- void Justification::InsetWidth( Rectangle& target, uint32 newWidth ) const
- {
- uint32 oldWidth = target.Width();
- if ( newWidth >= oldWidth )
- return;
-
- target.left += LeadingPortion( oldWidth, newWidth );
- target.right -= TrailingPortion( oldWidth, newWidth );
- Assert( stretchingPart > 0 || target.Width() == newWidth );
- }
-
- void Justification::InsetHeight( Rectangle& target, uint32 newHeight ) const
- {
- uint32 oldHeight = target.Height();
- if ( newHeight >= oldHeight )
- return;
-
- target.top += LeadingPortion( oldHeight, newHeight );
- target.bottom -= TrailingPortion( oldHeight, newHeight );
- Assert( stretchingPart > 0 || target.Height() == newHeight );
- }
-
- const Justification& Justification::Leading()
- {
- static Justification leading( 1, 0, 1 );
- return leading;
- }
-
- const Justification& Justification::Trailing()
- {
- static Justification trailing( 1, 1, 0 );
- return trailing;
- }
-
- const Justification& Justification::Center()
- {
- static Justification center( 2, 1, 1 );
- return center;
- }
-
- const Justification& Justification::Full()
- {
- static Justification full( 1, 0, 0 );
- return full;
- }
-